package ru.sviridov.sellandbuy;

import ru.sviridov.sellandbuy.model.IAuth;
import ru.sviridov.sellandbuy.model.query.QueryUpdateProfile;
import ru.sviridov.sellandbuy.tasks.base.BaseTask;
import ru.sviridov.sellandbuy.tasks.recipients.UpdateProfileRecipient;
import ru.sviridov.sellandbuy.tools.Constants;
import ru.sviridov.sellandbuy.url.APIGroup;
import ru.sviridov.sellandbuy.url.APIMethod;
import ru.sviridov.sellandbuy.url.URL;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import api.istsoft.ru.dal.Auth.Profile;

public class ProfileActivity extends Activity{
	private static final int REQUEST_ADDRESS = 1;
	
	private BaseTask<Void> task;
	private Profile profile;
	private String login;
	private String password;
	
	private EditText etLastName;
	private EditText etFirstName;
	private EditText etMiddleName;
	private EditText etContactPerson;
	private EditText etPhoneNumber;
	private EditText etAddress;
	private TextView tvPlace;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ContentViewMapper.setContentView(this);
		
		profile = loadProfile();
		configViews();
	}

	private void configViews() {
		tvPlace = (TextView)findViewById(R.id.tvPlace);
		etLastName = (EditText) findViewById(R.id.etLastName);
		etFirstName = (EditText) findViewById(R.id.etFirstName);
		etMiddleName = (EditText) findViewById(R.id.etMiddleName);
		etContactPerson = (EditText) findViewById(R.id.etContactPerson);
		etPhoneNumber = (EditText) findViewById(R.id.etPhoneNumber);
		etAddress = (EditText) findViewById(R.id.etAddress);
		
		etLastName.setText(profile.getLastName());
		etFirstName.setText(profile.getFirstName());
		etMiddleName.setText(profile.getMiddleName());
		etContactPerson.setText(profile.getContactPerson());
		etPhoneNumber.setText(profile.getPhoneNumber());
		etAddress.setText(profile.getAddress());
	}

	private Profile loadProfile() {
		SharedPreferences prefs = getSharedPreferences(Constants.USER_PREFS, Activity.MODE_PRIVATE);
		Profile profile = new Profile();
		profile.setCountryId(prefs.getInt("Profile_CountryId", -1));
		profile.setState(prefs.getInt("Profile_State", -1));
		profile.setCityId(prefs.getInt("Profile_CityId", -1));
		profile.setAddress(prefs.getString("Profile_Address", null));
		profile.setContactPerson(prefs.getString("Profile_ContactPerson", null));
		profile.setLastName(prefs.getString("Profile_LastName", null));
		profile.setFirstName(prefs.getString("Profile_FirstName", null));
		profile.setMiddleName(prefs.getString("Profile_MiddleName", null));
		profile.setPhoneNumber(prefs.getString("Profile_PhoneNumber", null));
		login = prefs.getString(IAuth.LOGIN, null);
		password = prefs.getString(IAuth.PASSWORD, null);
		
		return profile;
	}

	public void onClickChangePlace(View view){
		startActivityForResult(new Intent(this, ChooseAddressActivity.class), REQUEST_ADDRESS);		
	}

	public void onClickUpdate(View view){
		if(task!=null && task.getStatus()!=AsyncTask.Status.FINISHED)
			task.cancel(true);
		
		profile.setLastName(etLastName.getText().toString());
		profile.setFirstName(etFirstName.getText().toString());
		profile.setMiddleName(etMiddleName.getText().toString());
		profile.setContactPerson(etContactPerson.getText().toString());
		profile.setPhoneNumber(etPhoneNumber.getText().toString());
		profile.setAddress(etAddress.getText().toString());
		
		QueryUpdateProfile query = new QueryUpdateProfile();
		query.setAuthRequered(true);
		query.setLogin(login);
		query.setPassword(password);
		query.setProfile(profile);
		
		task = new BaseTask<Void>(this, new UpdateProfileRecipient());
		task.setQueryParams(query);
		task.execute(URL.getURL(APIGroup.MEMBERSHIP, APIMethod.UPDATE_PROFILE));
		Toast.makeText(this, "Updating", Toast.LENGTH_SHORT).show();
	}	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==REQUEST_ADDRESS && resultCode==RESULT_OK){
			Bundle bundle = data.getExtras();
			profile.setCityId(bundle.getInt(ChooseAddressActivity.CITY_ID, -1));
			profile.setState(bundle.getInt(ChooseAddressActivity.STATE_ID, -1));
			profile.setCountryId(bundle.getInt(ChooseAddressActivity.COUNTRY_ID, -1));
			String place = String.format(getString(R.string.format_address), bundle.getString(ChooseAddressActivity.CITY_NAME), bundle.getString(ChooseAddressActivity.STATE_NAME), bundle.getString(ChooseAddressActivity.COUNTRY_NAME));
			tvPlace.setText(place);
		}
	}
	
	public void onClickChangePassword(View view){
		startActivity(new Intent(this, ChangePasswordActivity.class));
	}
	
}

//<profile>
//<CountryId>long</CountryId>
//<State>long</State>
//<CityId>long</CityId>
//<Address>string</Address>
//<FirstName>string</FirstName>
//<LastName>string</LastName>
//<MidleName>string</MidleName>
//<ContactPerson>string</ContactPerson>
//<PhoneNumber>string</PhoneNumber>
//
//</profile>
